package likouJava;

import java.util.ArrayList;
import java.util.List;

public class 电话号码的字母组合 {

    private static final String[] KEYS = {
            "",        //0
            "",        //1
            "abc",     //2
            "def",     //3
            "ghi",     //4
            "jkl",     //5
            "mno",     //6
            "pqrs",    //7
            "tuv",     //8
            "wxyz",    //9
    };

    List<String> res = new ArrayList<>();
    StringBuilder path = new StringBuilder();

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0){
            return res;
        }
        backtrack(digits,0);
        return res;
    }

    private void backtrack(String digits, int index) {
        if (index == digits.length()){
            res.add(path.toString());
            return;
        }

        String letters = KEYS[digits.charAt(index) - '0'];
        for (char c: letters.toCharArray()){
            path.append(c);
            backtrack(digits, index + 1);
            path.deleteCharAt(path.length() - 1);
        }

    }

}
